package cn.ac.big.bigd.util;
import cn.ac.big.bigd.model.generate.NewAnnotation;
import cn.ac.big.bigd.model.generate.NewAnnotationDetail;
import cn.ac.big.bigd.model.generate.NewStatistics;
import cn.ac.big.bigd.model.generate.Nucleotide;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/7/14.
 */
public class TabUtil {
    public static ArrayList<String[]> readTab(String filepath, boolean head) throws Exception {
        ArrayList<String[]> tabList = new ArrayList<>(); //用来保存数据
        File file = new File(filepath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        if(head){
            br.readLine();
        }
        while ((line = br.readLine()) != null) { //循环读取行

            String[] segments = line.split("\t",-1); //按tab分割
            tabList.add(segments);
        }
        return tabList;
    }
    public static Map getStats(String filepath, boolean head) throws Exception {
        Map<String, Object> map = new HashMap();
        ArrayList<String[]> tabList = readTab(filepath, head);
        for (int row = 0; row < tabList.size(); row++) {

            String key = tabList.get(row)[0]; //取得第row行第0列的数据
            Object value = tabList.get(row)[1];
            map.put(key, value);

        }
        return map;
    }


    public static List<NewAnnotation> getAnnotation(String filepath, boolean head) throws Exception {
        List<NewAnnotation> newAnnotationList = new ArrayList<>();

        ArrayList<String[]> tabList = readTab(filepath, head);
        for (int row = 0; row < tabList.size(); row++) {
            NewAnnotation newAnnotation =new  NewAnnotation();
            newAnnotation.setName(tabList.get(row)[0]);
            newAnnotation.setProtein(tabList.get(row)[1]);
            newAnnotation.settRNA(tabList.get(row)[2]);
            newAnnotation.setrRNA(tabList.get(row)[3]);
            newAnnotation.setNcRNA(tabList.get(row)[4]);
            newAnnotation.setPseudogene(tabList.get(row)[5]);
            newAnnotation.setOthers(tabList.get(row)[6]);
            newAnnotation.setTotal(tabList.get(row)[7]);

             newAnnotationList.add(newAnnotation);

        }
        return newAnnotationList;
    }
    public static List<NewAnnotationDetail> getAnnotationDetail(String filepath, boolean head) throws Exception {
        List<NewAnnotationDetail> newAnnotationDetailList = new ArrayList<>();

        ArrayList<String[]> tabList = readTab(filepath, head);
        for (int row = 0; row < tabList.size(); row++) {
            NewAnnotationDetail newAnnotationDetail =new  NewAnnotationDetail();
            newAnnotationDetail.setName(tabList.get(row)[0]);
            newAnnotationDetail.setProtein(tabList.get(row)[1]);
            newAnnotationDetail.settRNA(tabList.get(row)[2]);
            newAnnotationDetail.setrRNA(tabList.get(row)[3]);
            newAnnotationDetail.setNcRNA(tabList.get(row)[4]);
            newAnnotationDetail.setPseudogene(tabList.get(row)[5]);
            newAnnotationDetail.setOthers(tabList.get(row)[6]);
            newAnnotationDetail.setTotal(tabList.get(row)[7]);

            newAnnotationDetailList.add(newAnnotationDetail);

        }
        return newAnnotationDetailList;
    }


    public static List<NewStatistics> getStatistics(String filepath, boolean head) throws Exception {
        List<NewStatistics> newStatisticsList = new ArrayList<>();

        ArrayList<String[]> tabList = readTab(filepath, head);
        for (int row = 0; row < tabList.size(); row++) {
            NewStatistics newStatistics =new  NewStatistics();
            newStatistics.setName(tabList.get(row)[0]);
            newStatistics.setValue(tabList.get(row)[1]);

            newStatisticsList.add(newStatistics);

        }
        return newStatisticsList;
    }

    public static void main(String[] args) throws Exception {
        List<NewAnnotationDetail>  newAnnotationList = getAnnotationDetail("D:/data/GWHAAAM00000000.whole.stats",true);
        System.out.println(newAnnotationList.toString());

    }
}
